package com.CTAtracker.android.Models;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Context;

import com.CTAtracker.android.DAO.FavoriteDataSource;
import com.CTAtracker.android.XML.InternetETA;

/**
 * The model of the Estimate Time of Arrival of a stop
 */
public class StopETA {

	/**
	 * The name of the stop
	 */
	private String _nameStop;
	/**
	 * The id of the stop
	 */
	private int _idStop;
	/**
	 * The id of the parent of the stop
	 */
	private int _parentId;
	/**
	 * The color of the line 
	 */
	private ColorStation _color;
	/**
	 * The list of the ETA 
	 */
	private ArrayList<InternetETA> _listInternetETA;
	/**
	 * The link to the database
	 */
	private FavoriteDataSource _favoriteDataSource;

	public StopETA(){
		_nameStop = "No name";
		_idStop = 0;
		_parentId= 0;
		_color = null;
		_listInternetETA = new ArrayList<InternetETA>();
	}

	public StopETA(Station station, Context context){
		_nameStop = station.get_stop_name();
		_idStop = station.get_stop_id();
		_parentId=station.get_parent_stop_id();
		_color = station.get_color();
		_listInternetETA = new ArrayList<InternetETA>();
		_favoriteDataSource = new FavoriteDataSource(context);
	}

	public String getNameStop(){
		return "To " + _nameStop.replaceAll("^(.*)\\((.*)\\)(.*)$", "$2");
	}
	
	public String getNameStopFavorite() {
		return _nameStop.replaceAll("^(.*)\\((.*)\\)(.*)$", "$1 to $2");
	}

	public void set_nameStop(String _nameStop) {
		this._nameStop = _nameStop;
	}

	public int get_idStop() {
		return _idStop;
	}

	public void set_idStop(int _idStop) {
		this._idStop = _idStop;
	}

	public int get_parentId() {
		return _parentId;
	}

	public void set_parentId(int _parentId) {
		this._parentId = _parentId;
	}

	/**
	 * Get the time of arrival 
	 * @return a string of all the ETA of a stop
	 */
	public String listToString(){
		String res = "";

		for (int i=0; i<_listInternetETA.size(); i++)
		{
			res += _listInternetETA.get(i).get_arrT() + "\n";
		}
		return res;
	}

	public ArrayList<InternetETA> get_listInternetETA() {
		return _listInternetETA;
	}

	public void set_listInternetETA(ArrayList<InternetETA> _listInternetETA) {
		this._listInternetETA = _listInternetETA;
	}

	public String getColorAPI() {
		return _color.getColorAPI();
	}

	public ColorStation getColor(){
		return _color;
	}

	/**
	 * Generate a String with the ETA
	 * @return the string with the ETAs
	 */
	public String getETA(){
		String ETA = "";
		for (int i=0; i < _listInternetETA.size();i++){
			ETA += etaToString(_listInternetETA.get(i)) + "\n";
		}
		return ETA;
	}

	@SuppressLint("SimpleDateFormat")
	private String etaToString(InternetETA internetETA) {
		if(internetETA.get_isApp() != 0)
			return "Due";
		if(internetETA.is_isDly())
			return "Delay";
		String res = "";
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
		Date dateRequest, ETA;
		try {
			dateRequest = dateFormat.parse(internetETA.get_prdt());
			ETA = dateFormat.parse(internetETA.get_arrT());
			long time = ETA.getTime()-dateRequest.getTime();
			int second = (int) (time/1000);
			int min = second/60;
			res = String.valueOf(min) + " minutes";
		} catch (ParseException e) {
			e.printStackTrace();
			res = "Unable to parse the Dates";
		}
		return res;
	}

	public String toString(){
		String res;

		res = "nameStop:"+_nameStop+"\n";
		res += "idStop:"+_idStop+"\n";
		res += "parentId:"+_parentId+"\n";
		res += "color:"+_color+"\n";
		res += "list ETA:"+_listInternetETA.toString()+"\n";

		return res;
	}

	public boolean isFavorite() {
		_favoriteDataSource.open();
		boolean isFavorite = _favoriteDataSource.isFavorite(_idStop);
		_favoriteDataSource.close();

		return isFavorite;
	}

	public void set_color(int color) {
		_color = new ColorStation(color);
	}


}
